package com.likianta.anykey;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by Likianta_DoDoRa on 2017/12/2 0002.
 */

public class SavedToMySharedPrefs {
    
    //建议在退出程序时调用，以将数据保存到文件中
    
    private SecuritySharedPreference securitySharedPreference;
    private SharedPreferences.Editor editor;
    
    public SavedToMySharedPrefs(Context context, String preferenceName) {
        securitySharedPreference = new SecuritySharedPreference(context, preferenceName, MODE_PRIVATE);
        editor = securitySharedPreference.edit();
    }
    
    /**
     * 获取cardList
     */
    
    public List<CardData> getCardData() {
        
        List<CardData> cardDataList = new ArrayList<>();
        
        String strJson = securitySharedPreference.getString("CardData", null);
        LogUtil.d("读取到的Json数组是：" + strJson);
        
        if (null == strJson) {
            return cardDataList;
        }
        
        Gson gson = new Gson();
        cardDataList = gson.fromJson(strJson, new TypeToken<List<CardData>>() {
        }.getType());
        
        return cardDataList;
    }
    
    /**
     * 保存cardList
     */
    public void setCardData(List<CardData> cardDataList) {
        
        if (null == cardDataList || cardDataList.size() <= 0) {
            return;
        }
        
        for (CardData cardData : cardDataList) {
            LogUtil.d("stmsp 即将保存的cardDataList是：" + cardData.getGroup().getGroupName());
        }
        
        Gson gson = new Gson();
        String strJson = gson.toJson(cardDataList); //转换成json数据，再保存
        
        LogUtil.d("stmsp 保存的strJson是：" + strJson);
        
        editor.clear();
        editor.putString("CardData", strJson);
        editor.apply();
    }
    
    /**
     * 获取userItemList
     */
    
    
    /**
     * 保存userItemList
     */
    
    
}
